<template>
    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="测评名" prop="assessmentName">
            <el-input v-model="form.assessmentName" placeholder="请输入测评名称" />
        </el-form-item>

        <el-form-item label="测评简介" prop="assessmentIntro">
            <el-input v-model="form.assessmentIntro" type="textarea" :rows="4" placeholder="请输入测评简介" />
        </el-form-item>

        <el-form-item label="列表图" prop="listImage">
            <el-input v-model="form.listImage" placeholder="请输入列表图链接" />
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup>
import { ref, defineProps } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';

const props = defineProps({
    form: {
        type: Object,
        default: () => {
            return {};
        }
    }
});

// 表单数据
const form = ref({
    assessmentName: '',
    assessmentIntro: '',
    listImage: '',
});
form.value = { ...props.form };

// 表单验证规则
const rules = {
    assessmentName: [{ required: true, message: '请输入测评名称', trigger: 'blur' }],
    assessmentIntro: [{ required: true, message: '请输入测评简介', trigger: 'blur' }],
    listImage: [{ required: true, message: '请输入列表图链接', trigger: 'blur' }],
};

const formRef = ref(null);

// 提交表单
const submitForm = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            await axios.post("/api/psychologicalAssessment/update", form.value);
            ElMessage.success('提交成功！');
            console.log('表单数据：', form.value);
        } else {
            ElMessage.error('请检查输入内容');
        }
    });
};

// 重置表单
const resetForm = () => {
    formRef.value.resetFields();
};
</script>

<style scoped>
.el-form {
    max-width: 500px;
    margin: 0 auto;
}
</style>
